<?php


namespace App\Http\Controllers\Users;



use App\Models\Banner;
use App\Models\Notice;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class HomeController extends BaseController
{

    /**
     * 轮播图列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function read(Request $request){

        //测试数据
        $data1 = [
            'https://demo1.19wms.com/storage/uploads/2020-06-04/202006041722535ed8bd6decc85.png',
            'https://demo1.19wms.com/storage/uploads/2020-06-04/202006041722535ed8bd6decc85.png',
            'https://demo1.19wms.com/storage/uploads/2020-06-04/202006041722535ed8bd6decc85.png'
        ];

        $items = [
            [
                'image'=>$data1[0]
            ],
            [
                'image'=>$data1[1]
            ],
            [
                'image'=>$data1[2]
            ]
        ];

        //查询banner数据
        $where = [];
        $get_banner = Banner::getlist($where);

        //图片路径处理
        if($get_banner){
            foreach ($get_banner as $k=>$v){
                $get_banner[$k]['image'] = url('/static/'.$get_banner[$k]['image']);
            }
            $items = $get_banner;
        }

        $data = [
            [
                'items'=>$items
            ]
        ];

        return $this->json(true,'OK',$data);

    }

    /**
     * 首页公告列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function notice(Request $request){

        //测试数据

        $test = [
            [
                'title'=>'标题',
                'content'=>'内容'
            ],
            [
                'title'=>'标题2',
                'content'=>'内容2'
            ]
        ];

        //获取数据库数据
        $where = [];
        $get_notice = Notice::where($where)
            ->get();

        $data = $get_notice;

        return $this->json(true,'OK',$data);

    }

    //通知详情
    public function detail(Request $request){

        $input =$request->only('id');

        $result = Validator::make($input,[
            'id'=>'required',
        ],[
            'id.required'=>'id不能为空！'
        ]);

        if($result->fails()){
            return  $this->json(1,$result->errors()->first());
        }

        //查询通知
        $where = [
            ['id',$input['id']]
        ];
        $get_notice = Notice::getfirst($where);

        $data = $get_notice;

        return $this->json(200,'OK',$data);
    }

}
